package actions;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;

import model.ReviewerModel;
import model.StudentModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;

public class ReviewApplicationAction extends Action {
    // is there a way to make this global for the application
    public final int REVIEW_PROCESS_THRESHOLD = 3;

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws DbException {
        // variables to be set to review
        String content = "";
        int grade = 0;
        int studentid = 0;
        int reviewerid = 0;
        // variable to check if there are enough reviews
        int numReviews = 0;
                
        // get review information from request object
        grade = Integer.parseInt((String)request.getParameter("grade"));
        content = request.getParameter("content");
        studentid = (Integer)request.getSession(false).getAttribute("studentid");
        reviewerid = ReviewerModel.fetchIdByUsername(request.getUserPrincipal().getName());
         
        // if this has never been graded before change the number of grades
        if(!ReviewerModel.alreadyGraded(reviewerid, studentid)){
            // decrement number of not reviewed
            ReviewerModel.incrementNotReviewed(reviewerid, -1);
            // increase number of reviewed
            ReviewerModel.incrementNumReviewed(reviewerid, 1);
            // increase student's numReviews
            StudentModel.incrementNumReviews(studentid);
        }
        
        // submit the grade/content to DB
        ReviewerModel.updateReview(grade, content, studentid, reviewerid);
        
        // check if there are enough reviews on this application
        numReviews = StudentModel.getNumGraded(studentid);
        
        if(numReviews < REVIEW_PROCESS_THRESHOLD){
            // gets ids of all users not graded
            ArrayList<Integer> reviewers = ReviewerModel.getSuggestedReviewers(studentid);
            // arraylist to store usernames of all reviewers who have not graded
            ArrayList<String> reviewersNotGraded = new ArrayList<String>();
            
            // convert from rewiewerid to username
            for(int i = 0; i < reviewers.size(); i++){
                String reviewer = ReviewerModel.fetchUsernameById(reviewers.get(i));
                reviewersNotGraded.add(reviewer);
            }
            
            request.getSession(false).setAttribute("reviewersNotGraded", reviewersNotGraded);
            return mapping.findForward("lessthanthree");
        }else{
            // get all the reviewers associated with the student
            ArrayList<ArrayList> reviewers = StudentModel.getReviewers(studentid);
            request.getSession().setAttribute("reviewers", reviewers);
            return mapping.findForward("threeormore");
        }

    }
}
